function BHVR_Plot2AFC_Generic(ax,schedule)
% BHVR_Plot2AFC_Generic(ax,schedule)
% 
% Plot running hit rate
% 
% DJS (2012)
rv = schedule.response_vals;

rcidx = regexp(schedule.readparams,'response_code');
rcidx = findincell(rcidx) + 1;
ttidx = regexp(schedule.readparams,'trial_type');
ttidx = findincell(ttidx) + 1;
% hpidx = findincell(regexp(schedule.readparams,'HPFreq'));

vals   = rv(:,rcidx); vals(isnan(vals))     = [];
trials = rv(:,ttidx); trials(isnan(trials)) = [];
% freqs  = rv(:,hpidx); freqs(isnan(freqs))   = [];


% hits = bitget(vals,6);

% LEFT: bit 3(2) ; RIGHT: bit 4(3)
% State = 1 (right correct)
Aleft  = bitget(vals,3) & bitget(vals,12);
Aright = bitget(vals,4) & bitget(vals,12);

% State = 0 (left correct)
Bleft  = bitget(vals,3) & bitget(vals,11);
Bright = bitget(vals,4) & bitget(vals,11);

Aind  = Aright|Aleft;
Bind  = Bleft |Bright;

if numel(Aind|Bind) == 1, return; end

% plot by trials
cla(ax)
hold(ax,'on')
grid(ax,'on')

Ay  = cumsum(Aright) ./ cumsum(Aind)  * 100;
By  = cumsum(Bleft)  ./ cumsum(Bind)  * 100;


plot(ax,find(Aind&Aright),  Ay(Aind&Aright), 'or','markersize',7,'markerfacecolor','r');
plot(ax,find(Bind&Bleft),   By(Bind&Bleft),  'ob','markersize',7,'markerfacecolor','b');
plot(ax,find(Aind&Aleft),   Ay(Aind&Aleft),  'xr','markersize',10);
plot(ax,find(Bind&Bright),  By(Bind&Bright), 'xb','markersize',10);

plot(ax,[0 length(trials)],[50 50],'-k');

xlim(ax,[0 length(trials)]); ylim(ax,[-10 110]);
set(ax,'ytick',[0 25 50 75 100]);
xlabel(ax,'Trial number');  ylabel(ax,'Hit rate');
box(ax,'on')
hold(ax,'off')
% legend(sprintf('AM (%0.1f%%)',AMhr),sprintf('Quiet (%0.1f%%)',Qhr),sprintf('Tone (%0.1f%%)',Thr), ...
%     'Overall','location','best')

